var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var hiUrl = require("url/hi");
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var helperOpen = require("helper/open");
var qqmath = require("util/qqmath");

//定义一个DateTimePicker对象
var do_DateTimePicker = sm("do_DateTimePicker")
// 本页面-ajax业务请求url
var httpUrl = hiUrl.url.result.page;
var noData = [{"template":1}];

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var listView = ui("listView");
var rootview = ui("$");
// 定义变量
var page = 1;
var labelStartTime=ui("labelStartTime");
var aLayoutStartTime=ui("aLayoutStartTime");
var labelEndTime=ui("labelEndTime");
var aLayoutEndTime=ui("aLayoutEndTime");
var do_Button_search=ui("do_Button_search");
//定义时间
var myDate=new Date(2017,01,01);
var date0 = myDate.getTime();

var myDate=new Date(2050,01,01);
var dateMax = myDate.getTime();

var searchBTime,searchETime;
//选择日期方式，最大日期为2050年最小日期为2001年，预设日期为当前时间，设置按钮文字
aLayoutStartTime.on("touch",function(){
	do_DateTimePicker.show({type:1, data:date0, minDate:date0,maxDate:dateMax, title:"日期选择", buttons:["取消","确定"]}, function(data, e){
        if(data.flag==1){
        	searchBTime=parseInt(data.time,10);
        	labelStartTime.text=timeFormat(searchBTime);
        }
    });
})
//选择日期方式，最大日期为2050年最小日期为2001年，预设日期为当前时间，设置按钮文字
aLayoutEndTime.on("touch",function(){
	do_DateTimePicker.show({type:1, data:date0,minDate:date0,maxDate:dateMax, title:"日期选择", buttons:["取消","确定"]}, function(data, e){
        if(data.flag==1){
        	searchETime=parseInt(data.time,10);
        	labelEndTime.text=timeFormat(searchETime);
        }
    });
})

function timeFormat(time){
	var svDate = new Date(time);//要格式化的时间
	return svDate.getFullYear()+"/"+(svDate.getMonth()+1)+"/"+svDate.getDate();
}
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

/* 头部 */
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	do_App.closePage()
});

//头部标题
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "荣誉记录",
})
header.refreshData();

// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	//读取当前页面的传入参数
	loadPage();
});
listView.bindItems(listdata);

do_Button_search.on("touch",function(data,e){
	loadPage();
})
// 刷新数据
function loadPage() {
	page = 1;
	var params = {
		page : page
	};
	if(searchBTime){
		params.startAt=searchBTime;
	}
	if(searchETime){
		params.endAt=searchETime;
	}
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if (data.page.content.length>0) {
			listdata.addData(data.page.content);
		}else{
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page
	};
	if(searchBTime){
		params.startAt=searchBTime;
	}
	if(searchETime){
		params.endAt=searchETime;
	}
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.addData(data.page.content);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});
